package com.graduate.game.assignment.controller;
import com.alibaba.fastjson.JSON;
import com.graduate.game.assignment.service.publishService;
import com.graduate.game.assignment.pojo.assignment;
import com.graduate.game.user.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Date;
import java.util.List;
import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("/assignment")
@CrossOrigin(origins = "*", maxAge = 3600)
public class publishController {
    @Autowired
    public publishService publishservice;
    //发布悬赏
    @RequestMapping("/publish")
    public String publish(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        assignment assignment = new assignment();
        Map maps =(Map) JSON.parse(s1);
        Date date = new Date(System.currentTimeMillis()-5400000);
        //用来表面该悬赏是否有人接，只能被一个人接
        assignment.setNumbers(0);
        assignment.setTitle((String) maps.get("title"));
        assignment.setContent((String) maps.get("content"));
        assignment.setGold(Integer.parseInt(maps.get("gold").toString()));
        assignment.setPublisher((String) maps.get("publisher"));
        assignment.setAnnex((String) maps.get("annex"));
        assignment.setDeletes(0);
        assignment.setTimes(date);
        publishservice.insert(assignment);

        return "发布成功";
    }
    //得到全部用户
    @RequestMapping("/getAll")
    public List<assignment> getAll(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        return publishservice.getAll();
    }
    @RequestMapping("/getoneByID")
    public assignment getOnedata(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s = request.getReader().readLine();
        System.out.println(s);
        assignment oneByID = publishservice.getOneByID(s);
        System.out.println(oneByID);
        return oneByID;
    }
    //模糊查询悬赏
    @RequestMapping("/serch")
    public List<assignment> serchUser(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        System.out.println("进入用户模糊查询");
        String s1 = request.getReader().readLine();
        return publishservice.serchassignment(s1);
    }
    //模糊查询悬赏
    @RequestMapping("/bypublisher")
    public List<assignment> bypublisher(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        return publishservice.getallbypublisher(s1);
    }

    //通过ID进行删除
    @RequestMapping("/delById")
    public String delBYID(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        System.out.println("进入删除");
        System.out.println("删除ID"+s1);
        publishservice.delById(Integer.parseInt(s1));
        return "删除成功";
    }

}
